package com.rongxunwu.netty.NioChat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

/**
 * TODO
 *
 * @author WuRongXun
 * @version 1.0
 * @date 2021/11/16 21:05
 */
public class NioChatClient {
    //服务器IP地址
    private static final  String host="127.0.0.1";
    //服务器端口
    private static final int port=6666;
    private Selector selector;
    private SocketChannel socketChannel;

    private String username;
    public NioChatClient(){
        try {
            selector=Selector.open();
            //连接服务器
            socketChannel=SocketChannel.open(new InetSocketAddress(host,port));
            //设置非阻塞
            socketChannel.configureBlocking(false);
            //将channel 注册到selector
            socketChannel.register(selector, SelectionKey.OP_READ);
            //得到username
            username = socketChannel.getLocalAddress().toString().substring(1);
            System.out.printf(username+"  is ok.....\n");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //向服务端发送消息
    public void SendInfo(String msg){
        msg=username+"  说: "+msg;
        try {
            socketChannel.write(ByteBuffer.wrap(msg.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //读取从服务器端回复的消息
    public void readInfo(){
        try {
            int count = selector.select();
            if (count>0){
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()){
                    SelectionKey nextKey = iterator.next();

                    //通道发送read 事件 即通道是可读状态
                    if (nextKey.isReadable()){
                        //得到相关通道
                        SocketChannel channel = (SocketChannel) nextKey.channel();
                        //得到buffer
                        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
                        //读取
                        channel.read(byteBuffer);
                        //把读取到的缓冲区数据转成字符串
                        String msg=new String(byteBuffer.array());
                        System.out.printf("\n"+msg.trim()+"\n");

                    }
                    //删除当前的selection
                    iterator.remove();
                }

            }else {
                //System.out.printf("没有数据可读》》》》》》\n");
            }
        } catch (IOException e) {
            System.out.printf(" \n服务器断开了》》》》》》");
        }
    }

    public static void main(String[] args) {
        //启动客户端
        NioChatClient nioChatClient=new NioChatClient();
        //启动一个线程
        new Thread(){
            @Override
            public void run(){
                while (true){
                    nioChatClient.readInfo();
                    try {
                        sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        //发送数据
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String s=scanner.nextLine();
            nioChatClient.SendInfo(s);
        }

    }
}
